﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers
{
    public class TrainController : BaseController
    {
        /// <summary>
        /// 培训首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 3);
            return View();
        }
        /// <summary>
        /// 获取培训列表
        /// </summary>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public JsonResult GetPageData(int pageSize, int pageIndex)
        {
            var orgz = Convert.ToInt32(Request.Params["orgz"]);
            var start = Request.Params["start"];
            var end = Request.Params["end"];
            var data = new TrainService().GetList(orgz, pageSize, pageIndex, start, end);
            
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 创建培训记录
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateTrain()
        {
            ViewData["type"] = new BaseItemService().GetListByType(4);
            return View();
        }
        /// <summary>
        /// 更新培训记录
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateTrain()
        {
            ViewData["type"] = new BaseItemService().GetListByType(4);
            ViewData["id"] = Request.Params["id"];
            ViewData["bType"] = Request.Params["type"];
            ViewData["time"] = Convert.ToDateTime(Request.Params["time"]).ToString("yyyy-MM-dd");
            return View();
        }
        /// <summary>
        /// 获取明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetTrainDetail(int id)
        {
            var data = new TrainDetailService().GetList(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存培训数据
        /// </summary>
        /// <returns></returns>
        public int SaveData()
        {
            var selType = Request.Params["selType"];
            var txtTime = Request.Params["txtTime"];
            var data = Request.Params["data"];
            var id = Request.Params["id"];
            Train ent = new Train();
            ent.BaseItemId = Convert.ToInt32(selType);
            ent.OrgzNo = UserLogin.OrgzNo;
            ent.Time = Convert.ToDateTime(txtTime);
            ent.UserId = UserLogin.UserId;
            JavaScriptSerializer json = new JavaScriptSerializer();
            var list = json.Deserialize<IList<TrainDetail>>(data);
            ent.TrainCount = list.Count;
            ent.TrainDetailList = list;
            if (!string.IsNullOrEmpty(id))
            {
                ent.Id = Convert.ToInt32(id);
                return new TrainService().Update(ent) ? 1 : 0;
            }
            return new TrainService().Add(ent);
        }
        /// <summary>
        /// 删除培训记录
        /// </summary>
        /// <param name="ids">ids</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            return new TrainService().DeleteList(ids) ? 1 : 0;
        }
        /// <summary>
        /// 培训详情
        /// </summary>
        /// <returns></returns>
        public ActionResult TrainDetail()
        {
            var type = Convert.ToInt32(Request.Params["type"]);
            ViewData["bTypeName"] = new BaseItemService().GetListByType(4).Where(it => it.Id == type).FirstOrDefault().Name;
            ViewData["id"] = Request.Params["id"];
            ViewData["time"] = Convert.ToDateTime(Request.Params["time"]).ToString("yyyy-MM-dd");
            return View();
        }
	}
}